package com.jumi.microservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jumi.microservice.common.base.BaseEntity;
import com.jumi.microservice.common.exception.BaseException;
import com.jumi.microservice.domain.app.AppVersion;
import com.jumi.microservice.mapper.AppVersionMapper;
import com.jumi.microservice.service.AppVersionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author ssk
 * @create 20/10/29 16:55
 **/
@Service
public class AppVersionServiceImpl implements AppVersionService {


    @Resource
    private AppVersionMapper appVersionMapper;


    @Override
    public int save(AppVersion appVersion) {
        AppVersion oldVersion=this.findLatestVersion();
        if(oldVersion!=null){
            if(this.compareNo(oldVersion.getH5AndroidNo(),appVersion.getH5AndroidNo())){
                throw new BaseException(500,"h5-安卓用版本号错误!");
            }
            if(this.compareNo(oldVersion.getH5IosNo(),appVersion.getH5IosNo())){
                throw new BaseException(500,"h5-IOS用版本号错误!");
            }
        }
        Date now = new Date();
        appVersion.setCreateTime(now);
        appVersion.setUpdateBy(appVersion.getCreateBy());
        appVersion.setUpdateTime(now);
        return appVersionMapper.insert(appVersion);
    }

    /**
     * 比较版本号是否存在错误,若老版本大于新版本返回true,反正false
     * @param oldNo
     * @param newNo
     * @return
     */
    private boolean compareNo(String oldNo,String newNo){
        String tempOld=oldNo.replace(".","");
        String tempNew=newNo.replace(".","");
        Long oldNum= null;
        Long newNum= null;
        try {
            oldNum = Long.parseLong(tempOld);
            newNum = Long.parseLong(tempNew);
        } catch (NumberFormatException e) {
            throw new BaseException(500,"版本号 "+newNo+" 不符合规则!");
        }
        if(oldNum>newNum)
            return true;
        else
            return false;
    }

    @Override
    public List<AppVersion> findAll(AppVersion appVersion) {
        Wrapper wrapper = createQueryConditions(appVersion);
        return appVersionMapper.selectList(wrapper);
    }

    @Override
    public AppVersion findById(Long versionId) {
        return appVersionMapper.selectById(versionId);
    }

    @Override
    public AppVersion findLatestVersion() {
        return appVersionMapper.findLatestVersion();
    }

    private Wrapper createQueryConditions(AppVersion appVersion) {
        LambdaQueryWrapper<AppVersion> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(appVersion.getH5AndroidNo()), AppVersion::getH5AndroidNo, appVersion.getH5AndroidNo());
        wrapper.like(StringUtils.isNotBlank(appVersion.getH5IosNo()), AppVersion::getH5IosNo, appVersion.getH5IosNo());
        wrapper.like(StringUtils.isNotBlank(appVersion.getAndroidNo()), AppVersion::getAndroidNo, appVersion.getAndroidNo());
        wrapper.like(StringUtils.isNotBlank(appVersion.getIosNo()), AppVersion::getIosNo, appVersion.getIosNo());
        wrapper.ge(StringUtils.isNotBlank(appVersion.getStartTime()), AppVersion::getUpdateTime, appVersion.getStartTime());
        wrapper.lt(StringUtils.isNotBlank(appVersion.getEndTime()), AppVersion::getUpdateTime, appVersion.getEndTime());
        wrapper.eq(AppVersion::getIsDel, 0);
        wrapper.orderByDesc(AppVersion::getVersionId);
        return wrapper;
    }
}
